﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FazaDemo.BL.AppFacade;
using FazaDemo.ViewModels;
using FazaDemo.BL.SubDomains;
using FazaDemo.PL.Mapping;

namespace FazaDemo.WebSiteMvc.Controllers
{
    public class NavigatorController : Controller
    {

        private readonly INavCondProvider _provider;

        private readonly IContainerElementMapper<NavCondElement, NavElementViewModel> _contMapper;

        private readonly ILowToTopMapper<NavigatorCondition, NavPageViewModel> _mapper;

        public NavigatorController()
        {
            _provider = new NavCondProvider();
            _contMapper = new ContainerElementMapper<NavCondElement, NavElementViewModel>();
            _mapper = new LowToTopMapper<NavigatorCondition, NavPageViewModel>();
        }

        public NavigatorController(INavCondProvider provider, IContainerElementMapper<NavCondElement, NavElementViewModel> contMapper, 
            ILowToTopMapper<NavigatorCondition, NavPageViewModel> mapper)
        {
            _provider = provider;
            _contMapper = contMapper;
            _mapper = mapper;
        }

        [HttpGet]
        public ActionResult Page(int? numEcq)
        {
            if (numEcq != null)
            {
                NavigatorCondition navCond = _provider.GetNavCondition((int)numEcq);
                NavPageViewModel model = GenerateViewModel(navCond);
                return PartialView(model);
            }
            return null;
        }

        private NavPageViewModel GenerateViewModel(NavigatorCondition navCond)
        {
            IEnumerable<NavElementViewModel> elemModels = _contMapper.MapFrom(navCond.CondElements);
            NavPageViewModel model = _mapper.MapFrom(navCond);
            model.NavArea = elemModels;
            return model;
        }

    }
}